package com.gking.centerSystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.Permissions;
import com.gking.centerSystem.service.Permissions.PermissionsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/permissions")
public class PermissionsController {

    @Resource
    private PermissionsService permissionsService;


    /**
     * 获取权限列表
     *
     * @param page   分页页码
     * @param size   分页大小
     * @param sortId 分类id
     * @return s
     */
    @RequiresPermissions("CS:manager:get:permissions")
    @GetMapping("/permissionsList")
    public Result<Page> getPermissionsList(@RequestParam Integer page, @RequestParam Integer size,
                                           @RequestParam String sortId) {
        return permissionsService.getPermissionsList(sortId, size, page);
    }

    /**
     * 创建权限或者修改权限信息
     *
     * @param permission 权限信息
     * @return s
     */
    @RequiresPermissions("CS:manager:set:permissions")
    @PostMapping("/setPermission")
    public Result<String> setPermission(@RequestBody Permissions permission) {

        String permissionId = permission.getId(); //可选参数，修改权限时可带上，创建时无需
        String name = permission.getName();
        String code = permission.getCode();
        String description = permission.getDescription();
        String sortId = permission.getSortId();

        return permissionsService.setPermission(name, code, description, sortId, permissionId);
    }

    /**
     * 批量删除权限
     *
     * @param permissionIdList 权限id列表
     * @return s
     */
    @RequiresPermissions("CS:manager:set:permissions")
    @PostMapping("/deletePermissions")
    public Result<String> deletePermissions(@RequestBody List<String> permissionIdList) {
        return permissionsService.deletePermissions(permissionIdList);
    }

    /**
     * 批量更新权限状态
     *
     * @param permissionIdList 权限id 列表
     * @param status           状态 0启用，1禁用
     * @return s
     */
    @RequiresPermissions("CS:manager:set:permissions")
    @PostMapping("/updatePermissionsStatus")
    public Result<String> updatePermissionsStatus(@RequestBody List<String> permissionIdList,
                                                  @RequestParam Integer status) {
        return permissionsService.updatePermissionsStatus(permissionIdList, status);
    }

}
